ajax({
    url:"http://egg.flower.dbice.cn/api/date",
    method:"GET",
    headers:{
        token:localStorage.getItem("token")
    }
}).then(res=>{
    console.log(res);
    let datatime = res.data
    datatime.forEach((item,index) => {
        let nowdata = Number(new Date())
        let timestamp = item.time;
        if(timestamp - nowdata <= 4320000000 && timestamp - nowdata > 0){
            let Month = String(new Date(Number(timestamp)).getMonth() + 1).padStart(2,0);
            let Hours = String(new Date(Number(timestamp)).getDay()).padStart(2,0);
            
            let option = document.createElement("option")
            option.innerHTML += `<option value="">${Month}月${Hours}日</option>`;
            option.value = item.id;
            Dataer.appendChild(option);
        }
    });
})

let spanlist = document.querySelector(".spanlist")
Dataer.addEventListener("change",function(){
    ajax({
        url:"http://egg.flower.dbice.cn/api/session",
        method:"GET",
        headers:{
            token:localStorage.getItem("token")
        },
        data:{
            timeId:Dataer.value
        }
    }).then(res=>{
        console.log(res);
        let datatime = res.data;
        spanlist.innerHTML = '';
        let arr = [];
        datatime.forEach((item,index) => {
            let starthour = String(new Date(Number(item.start_time)).getHours()) + ":".padEnd(3,0);
            let endthour = String(new Date(Number(item.end_time)).getHours()) + ":".padEnd(3,0);
            let state = item.is_appointment;
            let Session = item.id
            // console.log(Session);
            arr.push(state)
            let yuy;
            if(state === 1){
                yuy = "可预约"
            }else{
                yuy = "不可预约"
            }
            spanlist.innerHTML += `<div class="spanbox" data-id=${Session}><span>${starthour}-${endthour}</span><span class="spanyy"data-id=${Session}>${yuy}</span></div>`
        })

        let spanyy = document.querySelectorAll(".spanyy");
        // let spanbox = document.querySelectorAll(".  ")
        for(let i = 0; i < spanyy.length;i++){
            // console.log(spanyy[i].dataset.id);
            spanyy[i].addEventListener("click",function(e){
                ajax({
                    url:`http://egg.flower.dbice.cn/api/appointment/${localStorage.getItem("id")}`,
                    method:"PUT",
                    data:{
                        sessionId:spanyy[i].dataset.id,
                        isAppointment:'1'
                    },
                    headers:{
                        token:localStorage.getItem("token")
                    }
                }).then(res=>{
                    console.log(res);
                    localStorage.setItem("imgurl",res.data.qrcode)
                    spanyy[i].innerText = "已预约"
                    alert("预约成功")
                    localStorage.setItem("time",e.target.previousElementSibling.innerText)
                    localStorage.setItem("session_id",spanyy[i].dataset.id)
                    location="QGcode.html"
                }).catch(res=>{
                    console.log(res);
                    alert("这个场次不能预约")
                })
            })

        }
    })
})


